<template>
  <div class="app-container">
    <el-form ref="ruleForm" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>

      <el-form-item label="账号" prop="account">
        <el-input v-model="form.account" />
      </el-form-item>

      <el-form-item label="角色" prop="roleId">
        <el-select v-model="form.roleId" filterable placeholder="请选择角色" style="width: 100%;">
          <el-option v-for="(item, index) in roleList" :key="index" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="单位" prop="institutionId">
        <el-select v-model="form.institutionId" filterable placeholder="请选择单位" style="width: 100%;">
          <el-option v-for="(item, index) in institutionList" :key="index" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>

    </el-form>

    <div style="text-align: center;">
      <el-button type="primary" @click="onSubmit('ruleForm')">确认</el-button>
      <el-button @click="onCancel">关闭</el-button>
    </div>

  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  props: ['id'],
  data() {
    return {
      institutionList: [],
      roleList: [],
      form: {},
      buttonLoading: false,
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        institutionId: [
          { required: true, message: '请选择单位', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getUserById();
    this.getRoleList();
    this.getInstitutionList();
  },
  mounted() {

  },
  methods: {
    getUserById() {
      request({
        url: '/api/user/getUserById',
        method: 'get',
        params: { id: this.id }
      }).then((res) => {
        this.form = res.result;
      }).catch(() => {
        // console.log(err)
      })
    },
    getRoleList() {
      request({
        url: '/api/role/getRoleList',
        method: 'get',
        params: {}
      }).then((res) => {
        this.roleList = res.result;
      }).catch((err) => {
        this.$message(err)
      })
    },
    getInstitutionList() {
      request({
        url: '/api/institution/getInstitutionList',
        method: 'get',
        params: {}
      }).then((res) => {
        this.institutionList = res.result;
      }).catch((err) => {
        this.$message(err)
      })
    },
    onSubmit(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          request({
            url: '/api/user/updateUser',
            method: 'post',
            data: this.form
          }).then((res) => {
            this.buttonLoading = false;
            this.$message(res.resultCodeDesc);
            if (res.resultCode === '200') {
              this.$emit('refresh');
              this.$emit('closeDialog', false);
            }
          }).catch(() => {
            this.buttonLoading = false;
          })
        } else {
          this.$message({
            message: '表单验证失败',
            type: 'warning'
          })

          return false;
        }
      });
    },
    onCancel() {
      this.$emit('closeDialog', false);
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>
